package org.example.config;


import org.example.mapper.interceptor.TokenInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 拦截器配置类
 */
@Configuration //定义此类为配置类
public class InterceptorConfig implements WebMvcConfigurer {
    //配置拦截器
    public void addInterceptors(InterceptorRegistry registry) {
        String[] path = {"/**"};//拦截所有请求
        registry.addInterceptor(tokenInterceptor())
                .addPathPatterns(path) //配置拦截路径
                .excludePathPatterns("/wzz/login","/insertAdmin","/sendCode","/upload",
                        "/swagger-resources/**","/error/**","/v2/**","/alipay/pay","/alipay/notify","/wzz/pd","/wzz/sendCode","/wzz/register",
                        "/wzz/yz"); //排除路径
    }
    //配置拦截器
    @Bean
    public TokenInterceptor tokenInterceptor() {
        return new TokenInterceptor();
    }

    //配置跨域
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowCredentials(true)
                .allowedOriginPatterns("*")
                .allowedMethods("GET", "POST", "PUT", "DELETE")
                .allowedHeaders("*")
                .exposedHeaders("*");
    }
}
